//index.js
//获取应用实例
const app = getApp()
var ea = require('../../utils/each.js');

Page({
  data: {
    tiaojian:0,
    index:0,
    getindex: app.data.url +'/zhongxue/maoapi/getindex',
    lingdang: app.data.url +'/uploads/zhongxue/lingdang.png',
    youjiantou: app.data.url + "/uploads/zhongxue/youjiantou.png",
    changexiaoqu: app.data.url + '/zhongxue/maoapi/changexiaoqu',
    lunbo:{},
    guanggao:[],
    zuixin:[],
    xiaoqu:[],
    imagsmo:[
      { jishu: "guanyuwomen", name: "关于我们", src: app.data.url +"/uploads/zhongxue/gywms.png"},
      { jishu: "zuixinhuodong", name: "最新活动", src: app.data.url + "/uploads/zhongxue/szlls.png" },
      { jishu: "dakaibaoku", name: "公共宝库", src: app.data.url +"/uploads/zhongxue/dkbks.png" },
      { jishu: "lxwomen", name: "联系我们", src: app.data.url +"/uploads/zhongxue/sssssss.png" },
    ],
      
    indicatorDots: true,  //显示面板指示点
    autoplay: true,  // 自动轮播
    interval: 5000,
    duration: 1000,  
    circular:true, //衔接显示
    vertical:true,
    xiala: app.data.url + '/uploads/zhongxue/xiala.png',
    husourimge: app.data.url + '/uploads/zhongxue/sousuo.png'
  },
  
// 跳转搜索
  tosou:function(){
    wx.navigateTo({
      url: '/pages/indexsou/indexsou'
    })

  },

  tohwu:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index;
    var urls = this.data.lunbo.urls;
    if(urls.length>0){
      if (urls.length < index + 1) {
        index = 0;
      }
      console.log(urls[index]);
      wx.setStorageSync('urls', urls[index]);
      // return ;
      // return
      wx.navigateTo({
        url: '/pages/webview/webview',
      })
    }
  
  },
  changexiaoqus:function(){
    var user = wx.getStorageSync('user');
    var xiaoqu = wx.getStorageSync('xiaoqu');
    wx.request({
      url: this.data.changexiaoqu,
      data: {token:user.token,xiaoqu:xiaoqu},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res);
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  //跳转关于我们 
  guanyuwomen:function(){
    wx.navigateTo({
      url: '/pages/gywomen/gywomen'
    })
  },

  lxwomen:function(){
    wx.navigateTo({
      url: '/pages/lxwomen/lxwomen'
    })
  },

  dakaibaoku:function(){
   wx.switchTab({
     url: '/pages/checkpapers/checkpapers'
   })
  },

  zuixinhuodong:function(){
    wx.navigateTo({
      url: '/pages/zxactivity/zxactivity'
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //跳转到更多
  tobaoming:function(){
    wx.switchTab({
      url: '/pages/signup/signup',
    })
  },

  login:function(e){
    var id = e.currentTarget.dataset.id;
    var user =wx.getStorageSync('user');
    if(user.denglu==true){
   wx.navigateTo({
     url: '/pages/lgsigup/lgsigup?id='+id,
   })
    }else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  bindPickerChange:function(e){
    // console.log(e);
    var index = parseInt(e.detail.value);
    var xiaoqu = this.data.xiaoqu;
    var tiaojian = xiaoqu[index].id; 
    this.setData({index:index,tiaojian:tiaojian})
    // console.log(tiaojian)
    wx.setStorageSync('xiaoqu', tiaojian);
    this.getData();
    this.changexiaoqus();

  },
  onLoad: function (options) {
    wx.setStorageSync('ying', 1);
    


    this.getData();

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.changexiaoqus();
  },
  getData:function(){
    var that = this;
    var tiaojian = wx.getStorageSync('xiaoqu');
    wx.showLoading({
      title: '加载中',
    })
    console.log(tiaojian);
    wx.request({
      url: that.data.getindex,
      data: {xiaoqu:tiaojian},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          var lunbo = data.data.lunbotu;
          var guanggao = data.data.guanggao;
          var zuixin = data.data.zuixin;
          var xiaoqu = data.data.xiaoqu;
          that.setData({ lunbo: lunbo, guanggao: guanggao, zuixin: zuixin, xiaoqu: xiaoqu});
          var thisxiaoqu = wx.getStorageSync('xiaoqu');
          if (thisxiaoqu){
            ea.each(xiaoqu,function(i,v){
              if (v.id == thisxiaoqu){
                that.setData({index:i});
              }
            })
          }
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration:app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading();
      },
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onPullDownRefresh:function(){
    this.getData();
    wx.stopPullDownRefresh();
  },
  onShareAppMessage:function(){
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})
